import { defineConfig } from 'vite';
import { resolve } from 'path';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import Pages from 'vite-plugin-pages';
import {
	ElementPlusResolver,
	VueUseComponentsResolver,
} from 'unplugin-vue-components/resolvers';
import svgLoader from 'vite-svg-loader';

// https://vitejs.dev/config/
export default defineConfig({
	server: {
		host: true, // 可以以IP访问
		port: 8080, // 端口
		open: false, // 自动打开游览器
		cors: true, // 允许跨域
		proxy: {
			'/api': {
				// 这里配置真实的后端环境地址
				target: 'http://120.79.74.223:7101',
				changeOrigin: true,
				rewrite: (path) => path.replace('/api/', '/'),
			},
		},
	},
	build: {
		// 消除打包大小超过500kb警告
		chunkSizeWarningLimit: 2000,
		// 在生产环境移除console.log
		terserOptions: {
			compress: {
				drop_console: false,
				pure_funcs: ['console.log', 'console.info'],
				drop_debugger: true,
			},
		},
		assetsDir: 'static/assets',
		// 静态资源打包到dist下的不同目录
		rollupOptions: {
			output: {
				chunkFileNames: 'static/js/[name]-[hash].js',
				entryFileNames: 'static/js/[name]-[hash].js',
				assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
			},
		},
	},
	plugins: [
		vue(),
		Pages({
			// name 分隔符
			routeNameSeparator: '',
			exclude: [
				'**/components/**',
				'**/layout/**',
				'**/nested/**',
				'**/__tests__/**',
			],
		}),
		Icons({ autoInstall: true, compiler: 'vue3' }),
		svgLoader(),
		AutoImport({
			dts: './src/auto-imports.d.ts',
			dirs: [
				'src/store/**',
				'src/hooks/**',
				'src/constants',
				'src/utils/**',
				'src/types/**',
				'src/constants',
			],
			imports: [
				'vue',
				'pinia',
				'vue-i18n',
				'vue-router',
				'@vueuse/core',
				{ '@/locale': ['useLocale'] },
				{
					'element-plus': [
						'ElLoading',
						'ElMessage',
						'ElMessageBox',
						'ElNotification',
					],
				},
			],
			eslintrc: {
				enabled: true,
				filepath: './eslintrc-auto-import.json',
				globalsPropValue: true,
			},
			resolvers: [
				ElementPlusResolver(),
				IconsResolver({ prefix: 'Icon' }),
			],
		}),
		Components({
			dts: './src/components.d.ts',
			resolvers: [
				ElementPlusResolver({
					importStyle: 'sass',
				}),
				VueUseComponentsResolver(),
				IconsResolver(),
			],
		}),
	],
	resolve: {
		alias: {
			'@': resolve(__dirname, './src'),
		},
	},
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: `@use "@/styles/element/index.scss" as *;`,
			},
		},
	},
});
